package com.dyb.springcloud.controller;


import com.dyb.springcloud.entities.CommonResult;
import com.dyb.springcloud.entities.Payment;
import com.dyb.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payment" )
@Slf4j
public class PaymentController {

    @Autowired
    PaymentService paymentService;


    @Value(value = "${server.port}")
    private String serverPort; //获取端口号

    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("*********插入 payment{}"+payment);
        if (result > 0) {
            return new CommonResult(200,"插入数据库成功,端口号:" +serverPort,result);
        } else {
            return new CommonResult(500,"插入数据库失败,端口号:" +serverPort,null);
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询id{}"+id);
        if (payment != null) {
            return new CommonResult(200,"查询成功,端口号:" +serverPort,payment);
        } else {
            return new CommonResult(500,"没有对应记录,,端口号:" +serverPort+" 查询ID:"+id,null);
        }
    }

    @GetMapping("/lb")
    public String getServerPort(){
        return serverPort;
    }
}
